home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 12 / BBS in a box XII-2.iso / Files II / Prog / S / Shell.sit / Shell ƒ / Shell.c next >
Encoding:
C/C++ Source or Header  |  1989-11-05  |  10.5 KB  |  611 lines  |  [TEXT/KAHL]

  1.  
  2. /*
  3.     Application Shell  --  Version 1.00  --  Nov  5, 1989
  4.     
  5.     Copyright (c) 1989 by Neal E. Trautman
  6.     
  7.     'ShareWare' -- Please send $5 contribution to:
  8.         Neal Trautman
  9.         1701 S.W. 42nd Street
  10.         Fargo, ND  58103
  11.     
  12.     This is a simple application shell.
  13. */
  14.  
  15. #define    APPL_NAME    "\pApplication Shell"
  16. #define date        "\p11/5/89"
  17. #define version        "\pv1.00"
  18.  
  19. #define line1 "\p by Neal E. Trautman"
  20. #define line2 "\p'ShareWare'"
  21. #define line3 "\pPlease send $5 contribution to:"
  22. #define line4 "\pNeal Trautman"
  23. #define line5 "\p1701 S.W. 42nd Street"
  24. #define line6 "\pFargo, ND  58103"
  25. #define line7 version
  26. #define line8 date
  27.  
  28. #define WINDOWWIDTH     (INSET + 400 + INSET)
  29. #define WINDOWHEIGHT     (INSET + 200 + INSET)
  30. #define WINDOWTOP        40
  31. #define WINDOWLEFT        2
  32.  
  33. #define FAST         register
  34. #define NULL         0L
  35. #define MAXLONG     0xFFFFFFFFL
  36. #define BUTTON_OFF    0
  37. #define BUTTON_ON    1
  38. #define INSET        4
  39.  
  40. #define _Unimplemented    0xA89F    /* UnImplementedCore Routine Trap Number */
  41. #define _WaitNextEvent    0xA860    /* WaitNextEvent Trap Number            */
  42.  
  43. #define APPLE_ID    100
  44. #define FILE_ID        101
  45. #define EDIT_ID        102
  46.  
  47. enum {
  48.     A_ABOUT = 1
  49. };
  50.  
  51. enum {
  52.     F_NEW = 1,
  53.     F_OPEN,
  54.     F_CLOSE,
  55.     F_ULINE1,
  56.     F_SAVE,
  57.     F_SAVEAS,
  58.     F_REVERT,
  59.     F_ULINE2,
  60.     F_PSETUP,
  61.     F_PRINT,
  62.     F_ULINE3,
  63.     F_QUIT
  64. };
  65.  
  66. enum {
  67.     E_UNDO = 1,
  68.     E_ULINE1,
  69.     E_CUT,
  70.     E_COPY,
  71.     E_PASTE
  72. };
  73.  
  74. Boolean        Done,HasWNE;
  75. Rect        screenBounds;
  76. SysEnvRec    MacInfo;
  77. WindowPtr    Window1 = NULL;
  78.  
  79. #define    pstrlen(ps)            (int)(ps[0])
  80. #define    pcharcat(ps,ch)        ps[++(ps[0])] = ch
  81.  
  82. static void pstrcpy(d, s)
  83. StringPtr    d, s;
  84. {
  85.     BlockMove(s, d, s[0] + 1);
  86. }
  87.  
  88. static void pstrcat(d, s)
  89. StringPtr    d, s;
  90. {
  91.     BlockMove(s+1, d+(d[0])+1, s[0]);
  92.     d[0] += s[0];
  93. }
  94.  
  95.  
  96. static Boolean NextEvent(eventMask,theEvent)
  97.     int            eventMask;
  98.     EventRecord    *theEvent;
  99. {    
  100.     if (HasWNE)
  101.         {
  102.         return(WaitNextEvent(eventMask,theEvent,0L,NULL));
  103.         }
  104.     else
  105.         {
  106.         SystemTask();
  107.         return(GetNextEvent(eventMask,theEvent));
  108.         }
  109. }
  110.  
  111. #define doline(line)    \
  112.     {    MoveTo (rct.left + (rct.right - rct.left - StringWidth (line)) / 2,linev);    \
  113.         DrawString (line);    \
  114.         linev += lineinc;    }
  115.  
  116. static void drawabout(w)
  117. WindowPtr w;
  118. {    
  119.     Str255    str;
  120.     Rect    rct;
  121.     FAST int    linev,lineinc;
  122.  
  123.     linev = INSET + 20;
  124.     lineinc = INSET + 10;
  125.     rct = w->portRect;
  126.     InsetRect (&rct, INSET, INSET);
  127.     TextFont (systemFont);
  128.     TextSize (0);
  129.     pstrcpy(str,APPL_NAME);
  130.     pstrcat(str,line1);
  131.     doline (str);
  132.     TextFont (geneva);
  133.     TextSize (9);
  134.     doline ("\p");
  135.     doline (line2);
  136.     doline (line3);
  137.     TextFace(bold);
  138.     doline (line4);
  139.     doline (line5);
  140.     doline (line6);
  141.     TextFace(0);
  142.     MoveTo (rct.left + INSET, rct.bottom - INSET);
  143.     DrawString (line7);
  144.     MoveTo (rct.right - INSET - StringWidth (line8), rct.bottom - INSET);
  145.     DrawString (line8);
  146. }
  147.  
  148. static void doAbout()
  149. {
  150.     FAST WindowPtr    w;
  151.     EventRecord    evt;
  152.     Boolean        done;
  153.     int            h,v;
  154.     Rect        r;
  155.  
  156.     InitCursor ();
  157.     SetRect (&r, 0, 0, 240, 120);
  158.     w = NewWindow (NULL, &r, "\p", FALSE, dBoxProc, -1L, FALSE, 0);
  159.     SetPort (w);
  160.     h = screenBounds.left + (((screenBounds.right - screenBounds.left) -
  161.         (w->portRect.right - w->portRect.left)) / 2);
  162.     v = screenBounds.top  + (((screenBounds.bottom - screenBounds.top) -
  163.         (w->portRect.bottom - w->portRect.top)) / 3);
  164.     MoveWindow (w, h, v, FALSE);
  165.     ShowWindow (w);
  166.     /* drawabout (w); */
  167.     done = FALSE;
  168.     while (!done)
  169.         {
  170.         if (NextEvent (everyEvent, &evt))
  171.             switch (evt.what)
  172.                 {
  173.                 case mouseDown:            
  174.                     while (WaitMouseUp())
  175.                         ;        /* and fall thru... */
  176.                 case mouseUp:            
  177.                 case keyDown:
  178.                 case autoKey:
  179.                     done = TRUE;
  180.                     break;
  181.                 case updateEvt:
  182.                     if ((WindowPtr)(evt.message) == w)
  183.                         {
  184.                         SetPort (w);
  185.                         BeginUpdate (w);
  186.                         drawabout (w);
  187.                         EndUpdate (w);
  188.                         }
  189.                     break;
  190.                 }
  191.         }
  192.     DisposeWindow (w);
  193.     FlushEvents (mDownMask+mUpMask+keyDownMask+keyUpMask+autoKeyMask,0);
  194. }
  195.  
  196. static void DrawWindow(w)
  197.     WindowPtr    w;
  198. {
  199.     if (w == Window1)
  200.         {
  201.         EraseRect(&w->portRect);
  202.         DrawGrowIcon(w);
  203.         TextFont (systemFont);
  204.         TextSize (0);
  205.         MoveTo (w->portRect.left +
  206.             (w->portRect.right - w->portRect.left - StringWidth (APPL_NAME)) / 2,
  207.             w->portRect.top + (w->portRect.bottom - w->portRect.top) / 2);
  208.         DrawString(APPL_NAME);
  209.         }
  210. }
  211.  
  212. static void OpenWindow1()
  213. {
  214.     Rect    windowRect;
  215.  
  216.     SetRect(&windowRect, WINDOWLEFT, WINDOWTOP,
  217.         WINDOWLEFT + WINDOWWIDTH, WINDOWTOP + WINDOWHEIGHT);
  218.     Window1 = NewWindow(NULL, &windowRect, APPL_NAME, TRUE, zoomDocProc, -1, TRUE, NULL);
  219.     SetPort(Window1);
  220. }
  221.  
  222. static void DoCloseWindow(window)
  223.     WindowPtr    window;
  224. {
  225.     if (((WindowPeek)window)->windowKind < 0)
  226.         CloseDeskAcc(((WindowPeek)window)->windowKind);
  227.     else
  228.         DisposeWindow(window);
  229. }
  230.  
  231. static void SetupMenus()
  232. {
  233.     FAST MenuHandle    Menu;
  234.     Str255        s;
  235.  
  236.     s[0] = 1;
  237.     s[1] = appleMark;
  238.     Menu = NewMenu(APPLE_ID,s);
  239.     pstrcpy(s,"\pAbout ");
  240.     pstrcat(s,APPL_NAME);
  241.     pstrcat(s,"\p…/`;(-");
  242.     AppendMenu(Menu,s);
  243.     AddResMenu(Menu,'DRVR');
  244.     InsertMenu(Menu,APPLE_ID);
  245.     Menu = NewMenu(FILE_ID,"\pFile");
  246.     AppendMenu(Menu,
  247.         "\pNew/N;Open…/O;Close/W;(-;Save/S;Save As…/A;Revert/R;(-;Page Setup…;Print…/P;(-;Quit/Q");
  248.     InsertMenu(Menu,FILE_ID);
  249.     Menu = NewMenu(EDIT_ID,"\pEdit");
  250.     AppendMenu(Menu,"\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  251.     InsertMenu(Menu,EDIT_ID);
  252.     DrawMenuBar();
  253. }
  254.  
  255. pascal void resumeProc()
  256. {
  257.     ExitToShell();
  258. }
  259.  
  260. static Boolean InitMac()
  261. {
  262.     FAST int        i;
  263.     EventRecord        theEvent;
  264.     int                message,count;
  265.     AppFile            applfile;
  266.  
  267.     MaxApplZone();
  268.     MoreMasters();
  269.     MoreMasters();
  270.     MoreMasters();    
  271.     InitGraf (&thePort);
  272.     InitFonts ();
  273.     InitWindows ();
  274.     InitDialogs (resumeProc);
  275.     TEInit ();
  276.     InitMenus ();
  277.     InitCursor ();
  278.  
  279.         /* use screenBits.bounds to get all gDevices */
  280.     screenBounds = screenBits.bounds;
  281.     screenBounds.top += GetMBarHeight();
  282.     InsetRect(&screenBounds,INSET,INSET);
  283.  
  284.     HasWNE = (Boolean)
  285.         ((NGetTrapAddress(_WaitNextEvent,ToolTrap)) != (GetTrapAddress(_Unimplemented)));
  286.     if (HasWNE)
  287.         {    /* if Juggler, call WaitNextEvent() so our window comes up in front */
  288.         i = 8;
  289.         while (--i >= 0)
  290.             NextEvent(everyEvent,&theEvent);
  291.         }
  292.     FlushEvents (everyEvent,0);
  293.  
  294.     SysEnvirons(1, &MacInfo);
  295.     if (MacInfo.machineType <= envMac)
  296.         return(FALSE);
  297.     if (MacInfo.systemVersion < 0x0420)
  298.         return(FALSE);
  299.  
  300.     CountAppFiles(&message,&count);
  301.     if (count > 0)
  302.         {
  303.         for (i = 1; i <= count; i++)
  304.             {
  305.             GetAppFiles(i,&applfile);
  306.             /* do something with message and file */
  307.             }
  308.         }
  309.  
  310.     SetupMenus();
  311.     OpenWindow1();
  312.     
  313.     return(TRUE);
  314. }
  315.  
  316. static void HandleMenu(menuResult)
  317. long    menuResult;
  318. {
  319.     int            menu,item;
  320.     char        result;
  321.     Str255        daName;
  322.     GrafPtr        savePort;
  323.  
  324.     menu = HiWord(menuResult);
  325.     item = LoWord(menuResult);
  326.     switch (menu)
  327.         {
  328.         case APPLE_ID:
  329.             {
  330.             if (item == A_ABOUT)
  331.                 doAbout();
  332.             else
  333.                 {
  334.                 GetItem(GetMHandle(APPLE_ID), item, &daName);
  335.                 GetPort(&savePort);
  336.                 OpenDeskAcc(&daName);
  337.                 SetPort(savePort);
  338.                 }
  339.             break;
  340.             }
  341.         case FILE_ID:
  342.             {
  343.             switch (item)
  344.                 {
  345.                 case F_NEW: 
  346.                     {
  347.                     break;
  348.                     }
  349.                 case F_OPEN:    
  350.                     {
  351.                     break;
  352.                     }
  353.                 case F_CLOSE:
  354.                     {
  355.                     break;
  356.                     }
  357.                 case F_SAVE:    
  358.                     {
  359.                     break;
  360.                     }
  361.                 case F_SAVEAS:    
  362.                     {
  363.                     break;
  364.                     }
  365.                 case F_REVERT:    
  366.                     {
  367.                     break;
  368.                     }
  369.                 case F_PSETUP:    
  370.                     {
  371.                     break;
  372.                     }
  373.                 case F_PRINT:    
  374.                     {
  375.                     break;
  376.                     }
  377.                 case F_QUIT:    
  378.                     {
  379.                     Done = TRUE;
  380.                     break;
  381.                     }
  382.                 }
  383.             break;
  384.             }
  385.         case EDIT_ID:
  386.             {
  387.             if (!SystemEdit(item - 1))
  388.                 {
  389.                 switch(item)
  390.                     {
  391.                     case E_UNDO:    
  392.                     case E_CUT:    
  393.                     case E_COPY:    
  394.                     case E_PASTE:    
  395.                         {
  396.                         SysBeep(2L);
  397.                         break;
  398.                         }
  399.                     }
  400.                 }
  401.             break;
  402.             }
  403.         }
  404.     HiliteMenu(0);
  405. }
  406.  
  407. static void HandleMouseUp(theEvent)
  408.     EventRecord        *theEvent;
  409. {
  410. }
  411.         
  412. static void HandleContent(theEvent,window)
  413.     EventRecord    *theEvent;
  414.     WindowPtr    window;
  415. {
  416. }
  417.  
  418. static void HandleMouseDown(theEvent)
  419.     FAST EventRecord    *theEvent;
  420. {
  421.     WindowPtr    mwdw;
  422.     Rect        growrect;
  423.     FAST int    wherecode;
  424.     FAST long    tmplong;
  425.     FAST WindowPtr    mouseWindow;
  426.  
  427.     wherecode = FindWindow(theEvent->where,&mwdw);
  428.     mouseWindow = mwdw;
  429.     switch(wherecode)
  430.         {
  431.         case inMenuBar:
  432.             {
  433.             HandleMenu(MenuSelect(theEvent->where));
  434.             break;
  435.             }
  436.             
  437.         case inSysWindow:
  438.             {
  439.             SystemClick(theEvent,mouseWindow);
  440.             break;
  441.             }
  442.  
  443.         case inContent:
  444.             {
  445.             if (mouseWindow != FrontWindow())
  446.                 SelectWindow(mouseWindow);
  447.             HandleContent(theEvent,mouseWindow);
  448.             break;
  449.             }
  450.             
  451.         case inDrag:
  452.             {
  453.             DragWindow(mouseWindow,theEvent->where,&screenBounds);
  454.             break;
  455.             }
  456.  
  457.         case inGoAway:
  458.             {
  459.             if (TrackGoAway(mouseWindow,theEvent->where))
  460.                 DoCloseWindow(mouseWindow);
  461.             break;
  462.             }
  463.  
  464.         case inGrow:
  465.             {
  466.             if (mouseWindow != FrontWindow())
  467.                 {
  468.                 SelectWindow(mouseWindow);
  469.                 }
  470.             else
  471.                 {
  472.                 if (theEvent->modifiers & optionKey)
  473.                     ZoomWindow(mouseWindow,inZoomOut,TRUE);
  474.                 else
  475.                     {
  476.                     SetRect(&growrect,75,75,32000,32000);
  477.                     tmplong = GrowWindow(mouseWindow, theEvent->where, &growrect);
  478.                     if (tmplong != 0)
  479.                         {
  480.                         SetPort(mouseWindow);
  481.                         InvalRect(&mouseWindow->portRect);
  482.                         SizeWindow(mouseWindow, LoWord(tmplong), HiWord(tmplong), TRUE);
  483.                         }
  484.                     }
  485.                 }
  486.             break;
  487.             }
  488.             
  489.         case inZoomIn:
  490.         case inZoomOut:
  491.             {
  492.             if (TrackBox(mouseWindow,theEvent->where,wherecode))
  493.                 ZoomWindow(mouseWindow,wherecode,TRUE);
  494.             break;
  495.             }
  496.         }
  497. }
  498.  
  499. static void HandleKeyBoard(theEvent)
  500.     EventRecord    *theEvent;
  501. {
  502.     char    ch;
  503.     
  504.     ch = (char)(theEvent->message & charCodeMask);
  505. }
  506.  
  507. static void HandleUpdate(theEvent)
  508.     EventRecord        *theEvent;
  509. {
  510.     FAST WindowPtr        window;
  511.  
  512.     window = (WindowPtr)theEvent->message;
  513.     SetPort(window);
  514.     BeginUpdate(window);
  515.     DrawWindow(window);
  516.     EndUpdate(window);
  517. }
  518.  
  519. static void HandleActivate(theEvent)
  520.     EventRecord        *theEvent;
  521. {
  522.     WindowPtr         window;
  523.     Boolean            doActive;
  524.     
  525.     window = (WindowPtr)theEvent->message;
  526.     doActive = ((theEvent->modifiers & activeFlag) != 0);
  527.     DrawGrowIcon(window);
  528. }
  529.  
  530. static void HandleDisk(theEvent)
  531.     EventRecord    *theEvent;
  532. {
  533.     Point        where;
  534.     
  535.     if (HiWord(theEvent->message) != noErr)
  536.         {
  537.         DILoad();
  538.         SetPt(&where, 115, 70);
  539.         DIBadMount(where,theEvent->message);
  540.         DIUnload();
  541.         }
  542. }
  543.  
  544. static void HandleNull(theEvent)
  545.     EventRecord    *theEvent;
  546. {
  547. }
  548.  
  549. static void doEvent()
  550. {    
  551.     EventRecord            theEventRecord;
  552.     FAST EventRecord    *theEvent = &theEventRecord;
  553.     
  554.     NextEvent(everyEvent,theEvent);
  555.     switch (theEvent->what)
  556.         {
  557.         case mouseDown:
  558.             {
  559.             HandleMouseDown(theEvent);
  560.             break;
  561.             }      
  562.         case mouseUp:
  563.             {
  564.             HandleMouseUp(theEvent);
  565.             break;
  566.             }
  567.         case keyDown:
  568.         case autoKey:
  569.             {
  570.             if (theEvent->modifiers & cmdKey)
  571.                 HandleMenu(MenuKey((char)(theEvent->message & charCodeMask)));
  572.             else
  573.                 HandleKeyBoard(theEvent);
  574.             break;
  575.             }
  576.         case updateEvt:
  577.             {
  578.             HandleUpdate(theEvent);
  579.             break;
  580.             }
  581.         case activateEvt:
  582.             {
  583.             HandleActivate(theEvent);
  584.             break;
  585.             }
  586.         case diskEvt:
  587.             {
  588.             HandleDisk(theEvent);
  589.             break;
  590.             }
  591.         case nullEvent:
  592.             {
  593.             HandleNull(theEvent);
  594.             break;
  595.             }
  596.         }
  597. }
  598.  
  599. void main()
  600. {
  601.     if (InitMac())
  602.         {
  603.         Done = FALSE;
  604.         while(!Done)
  605.             doEvent();
  606.         }
  607.     else
  608.         SysBeep(2L);
  609. }
  610.  
  611.